/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cart.ejb;

import static java.lang.System.out;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.ejb.Singleton;
import javax.ejb.Startup;

@Startup
@Singleton
public class AppBean implements App {
    private static int appCount = 0;
    private static int cartCount = 0;
    private static int cartMax = 0;
    
    private int appId = -1;
    
    @PostConstruct
    void init() {

        appCount++;
        appId = appCount;
//        new Thread(){
//            public void run(){
//                while(true){
//                  sayHello();
//                    try {
//                        Thread.sleep(5000);
//                    } catch (InterruptedException ex) {
//                        Logger.getLogger(AppBean.class.getName()).log(Level.SEVERE, null, ex);
//                    }
//                }
//                
//            }
//        }.start();
        out.println("AppBean "+appId+" init");
        
    }
    
    @PreDestroy
    void destroy() {
        appCount--;
        out.println("AppBean "+appId+"destroied.");
    }

    @Override
    public int getAppInstanceNum() {
        return appCount;
    }

    @Override
    public int getAppId() {
        out.println("getAppId()");
//        try {
//            Thread.sleep(50000);
//        } catch (InterruptedException ex) {
//            Logger.getLogger(AppBean.class.getName()).log(Level.SEVERE, null, ex);
//        }
        return appId;
    }

    @Override
    public int getCartInstanceNum() {
        return cartCount;
    }

    @Override
    public int createdCart() {
        cartCount++;
        cartMax++;
        return cartMax;
    }

    @Override
    public void removeCart() {
        cartCount--;
    }
    
    private void sayHello(){
        out.println("hello!");
    }

}
